package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AOrders;
import com.ruoyi.system.domain.OrdersVo;
import com.ruoyi.system.domain.Vo.OrderDetailsVo;
import com.ruoyi.system.service.IAOrdersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 * 
 * @author 王家宁
 * @date 2024-09-02
 */
@Controller
@RequestMapping("/system/orders")
public class AOrdersController extends BaseController
{
    private String prefix = "system/orders";

    @Autowired
    private IAOrdersService aOrdersService;



    @GetMapping()
    public String orders()
    {
        return prefix + "/orders";
    }


    /**
     * 查询订单列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AOrders aOrders)
    {
        startPage();
        aOrders.setIsDelete(0);
        List<OrdersVo> list = aOrdersService.selectAOrdersList(aOrders);
        return getDataTable(list);
    }

    // 在OrdersController中添加
// 发货操作
    @PostMapping("/ship/{orderId}")
    @ResponseBody
    @RequiresPermissions("system:orders:edit") // 根据实际权限配置调整
    public AjaxResult ship(@PathVariable Long orderId) {
        return updateOrderStatus(orderId, 1L); // 假设2是发货状态
    }

    @PostMapping("/confirm/{orderId}")
    @ResponseBody
    @RequiresPermissions("system:orders:edit")
    public AjaxResult confirm(@PathVariable Long orderId) {
        return updateOrderStatus(orderId, 2L); // 假设3是收货状态
    }

    private AjaxResult updateOrderStatus(Long orderId, Long status) {
        AOrders orders = new AOrders();
        orders.setId(orderId);
        orders.setStatus(status);
        int rows = aOrdersService.updateAOrders(orders);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("操作失败");
    }



    /**
     * 导出订单列表
     */
    @RequiresPermissions("system:orders:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AOrders aOrders)
    {
        List<OrdersVo> list = aOrdersService.selectAOrdersList(aOrders);
        ExcelUtil<OrdersVo> util = new ExcelUtil<OrdersVo>(OrdersVo.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("system:orders:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AOrders aOrders)
    {
        return toAjax(aOrdersService.insertAOrders(aOrders));
    }

    /**
     * 修改订单
     */
    @GetMapping("/read/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderDetailsVo orderDetails = aOrdersService.getOrderDetails(id);
        mmap.put("orderDetails", orderDetails);
        return prefix + "/orders_details";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("system:orders:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AOrders aOrders)
    {
        return toAjax(aOrdersService.updateAOrders(aOrders));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("system:orders:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(aOrdersService.deleteAOrdersByIds(ids));
    }
}
